
import { WbComponentDatas } from '@/store/default-wb-datas';
import { CONTROL_TYPES, CALENDER_TYPE_MAP } from '@/common/const'
import { CONTROL_WB_ATTR, COMPONENT_TYPES } from '@/common/const-wb';

import { getMainEventControlId } from '@/store/utils';

// initial state
const state = {
  controls: {},
  currentControls: null,
}
export function dealVoteList(){
  if(!state.currentControls){
    return
  }
  let voteItem = state.currentControls[CONTROL_TYPES.vote] //1001
  if(voteItem){
    let lists
    let componentData = state.currentData
    let mainControlId = getMainEventControlId(componentData.events)
    if(mainControlId){
      if(state.currentControls[mainControlId].data.elements){
        lists = state.currentControls[mainControlId].data.elements
      }
    }else{
      lists = componentData.lists
    }
    _.forEach(lists, (item, index) => {
      item.voteId = index + 1
    })
    if(componentData.cid == COMPONENT_TYPES.wb_calendar){
      if(!lists){
        lists = [];
      }
      while(lists.length > 0){
        lists.pop();
      }

      let dateStr = "";
      if(componentData.commonAttr.calendarType == CALENDER_TYPE_MAP.currentMonth){
        let date = new Date();
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        dateStr = year + "/" + month + "/" + 1;
        componentData.commonAttr.minDate = dateStr;
      }else{
        dateStr = componentData.commonAttr.minDate;
      }
      let arr = dateStr.split("/");
      let month = arr[1];
      let year = arr[0];

      let d = new Date(year, month, 0);
      let days = d.getDate();
      let month_ = '';
      let day_ = '';
      for(let i = 1; i <= days; i++) {
        month_ = month <= 9 ? "0" + month.toString() : month.toString();
        day_ = i <= 9 ? "0" + i.toString() : i.toString();
        lists.push({ voteId: year.toString() + month_ + day_ });
      }
    }

    let voteElements = voteItem.data.elements
    let key
    let voteObj = {}
    if(_.isArray(lists)){
      _.forEach(lists, (item, index) => {
        /* eslint-disable */
        if(item.hasOwnProperty('voteId')){
          key = item['voteId']
        }else {
          item.voteId = _.toString(index+1)
          key = item.voteId
        }
        /* eslint-disable */
        if(voteElements.hasOwnProperty(key)){
          voteObj[key] = voteElements[key]
        }else{
          if(key > 0){
            voteObj[key] = {'base':0,'total':0,'num': 0, 'progress':0}
          }
        }
      })
    }else{
      let index = 0
      key = _.toString(index + 1)
      /* eslint-disable */
      if(voteElements.hasOwnProperty(key)){
        voteObj[key] = voteElements[key]
      }else{
        if(key.length > 0){
          voteObj[key] = {'base':0,'total':0,'num': 0, 'progress':0}
        }
      }
    }
    voteItem.data.elements = voteObj
  }
}
// getters
const getters = {
}

// actions
const actions = {
  addWbComponentItem: (store, params) => {
    let {commit} = store;
    commit('addWbComponentItem', params);
  },
  updateWbComponentItem: ({commit}, params) => {commit('updateWbComponentItem', params)},
  deleteWbComponentItem: (store, params) => {
    let {commit} = store;
    commit('deleteWbComponentItem', params)
  },
  updateWbComponent: ({commit}, data) => {commit('updateWbComponent', data)},
  deleteControl: ({commit}, data) => {commit('deleteControl', data)},
}

// mutations
const mutations = {
  setControlById(state, {compId, newCompId}) {
    let controlData = state.controls[compId];
    delete state.controls[compId];
    state.controls[newCompId] = controlData;
  },
  setControls(state, data){
    state.controls = {};
    for(let id in data){
      state.controls[id] = data[id];
    }
  },
  setActiveWbData(state, currentData){
    //页面或是组件数据
    state.currentData = currentData
    if(currentData && currentData.id){
      /* eslint-disable */
      if(state.controls.hasOwnProperty(currentData.id)){
        state.currentControls = state.controls[currentData.id];
      }else{
        // Vue.set(state.controls, currentData.id, {});
        state.controls[currentData.id] = {}
        state.currentControls = state.controls[currentData.id];
      }
    }else{
      //删除组件
      state.currentControls = null;
    }
  },
  addEventControlToControls(state, { copyId, compId }){
    state.controls[compId] = _.cloneDeep(state.controls[copyId])
  },
  addEventControlToCurrentControls(state, { controlId, controlData }){
    if(!WbComponentDatas[controlId]){
      return;
    }
    let controlData_ = _.cloneDeep(WbComponentDatas[controlId]);
    if(controlData && _.isArray(controlData_.data.elements)){
      let controlItemData = controlData_.data.elements.pop();
      _.forEach(controlData.data.elements, (itemData) => {
        let copyItem = _.cloneDeep(controlItemData)
        _.merge(copyItem, itemData)
        controlData_.data.elements.push(copyItem)
      })
    }
    if(CONTROL_WB_ATTR[controlId]){
      let wbAttr = _.cloneDeep(CONTROL_WB_ATTR[controlId].wbAttr);
      controlData_['wbAttr'] = wbAttr;
    }

    if(state.currentControls[controlId]){
      state.currentControls[controlId] = controlData_;
    }else{
      state.currentControls[controlId] = controlData_
      // Vue.set(state.currentControls, controlId, controlData_);
    }
    dealVoteList()
  },
  deleteEventControlToControls(state, {event, id}){
    let controlId = event.controlId
    delete state.currentControls[controlId];

    if(_.size(state.currentControls) == 0){
      delete state.controls[id]
    }
  },
  addWbComponentItem (state, { controlId, item }) {
    let wbList = state.currentControls[controlId].data.elements
    wbList.push(item);
    dealVoteList()
  },
  updateWbComponentItem(state, {index, item, controlId}){
    let wbList;
    if(state.currentControls[controlId].data.elements){
      wbList = state.currentControls[controlId].data.elements;
    }else{
      wbList = state.currentControls[controlId].data;
    }
    if(_.isString(item)){
      wbList[index] = item;
    }else if(controlId == CONTROL_TYPES.wb_push && item.pics){
      if(wbList[index].pics.length != item.pics.length){
        wbList[index].pics = []
      }
      _.merge(wbList[index], item);
    }else{
      _.merge(wbList[index], item);
    }
    dealVoteList()
    // wbList[index] = _.cloneDeep(wbList[index]);//这个代码也是为了强制刷新ui
  },
  deleteWbComponentItem(state,  {index, controlId}){
    let wbList = state.currentControls[controlId].data.elements;
    wbList.splice(index, 1);
    dealVoteList()
  },
  updateWbComponent(state, {data, controlId}){
    _.merge(state.currentControls[controlId].data, data);
  },
  updateWbAttr(state, {wbAttr, controlId}){
    _.merge(state.currentControls[controlId].wbAttr, wbAttr);
  },
  deleteControl(state, id){
    delete state.controls[id];
  },
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
